package cn.sccl.common.util;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by wangping on 2017/1/19.
 */
public class HttpUtils {
    private static HttpUtils instance;

    public static HttpUtils getInstance(){
        if (instance == null){
            instance = new HttpUtils();
        }
        return instance;
    }

    /**
     * 判断一个request是否为ajax请求
     * @param request
     * @return
     */
    public boolean isAjaxRequest(HttpServletRequest request){
        String header = request.getHeader("x-requested-with");
        if ("XMLHttpRequest".equalsIgnoreCase(header)){
            return true;
        }else {
            return false;
        }
    }

    /** 获取机器名 */
    public static final String getHostName() {
        String hostName = "";
        try {
            hostName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {

        }
        return hostName;
    }
}
